package nobnak.study.gl;

import java.util.Iterator;
import java.util.List;

import nobnak.study.gl.BezierCurveRenderer.VertexBufferElement;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class PluggableGLSurfaceView extends GLSurfaceView {
	private BezierCurveRenderer renderer;
	private OnTouchListener touchListener;
	
	// Constructors
	public PluggableGLSurfaceView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	public PluggableGLSurfaceView(Context context) {
		super(context);
		init();
	}
	
	// Interfaces
	public interface RenderTask {
		void execute();
	}
	
	// Methods
	public void init() {
		this.renderer = new BezierCurveRenderer();
        setRenderer(renderer);        
    }
	public RenderTask makeTaskAddVertexElement(final VertexBufferElement element) {
		return new RenderTask() {
			@Override
			public void execute() {
				renderer.addVertexElement(element);
			}
		};
	}
	public RenderTask makeTaskRemoveVertexElement(final VertexBufferElement element) {
		return new RenderTask() {
			@Override
			public void execute() {
				renderer.removeVertexElement(element);
			}
		};
	}
	public void doTasks(final List<RenderTask> tasks) {
		queueEvent(new Runnable() {
			@Override
			public void run() {
				for (Iterator<RenderTask> iter = tasks.iterator(); iter.hasNext(); )
					iter.next().execute();
			}
		});
	}
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (touchListener != null && touchListener.onTouch(this, event))
			return true;
		return super.onTouchEvent(event);
	}
	@Override
	public void setOnTouchListener(OnTouchListener l) {
		super.setOnTouchListener(l);
		touchListener = l;
	}
	
}
